package com.mindao.app.ued.controller;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mindao.app.common.spring.controller.BaseController;
import com.mindao.app.common.spring.model.BaseModel;
import com.mindao.app.common.spring.service.util.JsonResult;
import com.mindao.app.ued.model.Event;
import com.mindao.app.ued.model.EventPrize;
import com.mindao.app.ued.service.EventPrizeService;
import com.mindao.utils.config.EaCommonUtils;

/**
/* 版权所有： 广州敏道科技有限公司
/*
/* 功能描述：
/*
/* 创 建 人：李国才
/* 创建时间：2016年6月25日 下午12:02:58 	
 **/@Controller
 @RequestMapping("/ued/eventprize2")
public class EventPrize2Controller extends BaseController<EventPrize> {
		private final Logger log = LoggerFactory.getLogger(EventPrize2Controller.class);
		@Autowired
		protected EventPrizeService eventPrizeService;
		
		/**
		 * 保存子表记录
		 * @param request
		 * @param response
		 * @param session
		 * @param rhs
		 * @return
		 * @throws Exception
		 */
	    //@ResponseBody
		@RequestMapping( method = RequestMethod.POST)
		public String saveSub(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs,EventPrize ep) throws Exception {
			String mainUuid=getpara("mainUuid");
			Event event=(Event)EaCommonUtils.getObjectByUuid(mainUuid, "Event");
			ep.setEvent(event);
	    	if (ep.getId()==null){
	    		baseService.create(ep);
	    	}else{
	    		baseService.update(ep);
	    	}
	    	
	    	return super.redirectWithNewParam(request, response, "saveSub", "listSub","&mainUuid="+mainUuid);
	    	//JsonResult jr=new JsonResult(JsonResult.SUCCESS,"保存成功");
			//return jr;
		}
		/**
		 * 加载子表记录
		 * @param request
		 * @param response
		 * @param session
		 * @param rhs
		 * @param ep
		 * @return
		 * @throws Exception
		 */
		@RequestMapping( method = RequestMethod.GET)
		public String editSub(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs,String subUuid) throws Exception {
			if (!StringUtils.isBlank(subUuid)){
				EventPrize ep=(EventPrize)EaCommonUtils.getObjectByUuid(subUuid, "EventPrize");
				rhs.addAttribute("sub",ep);
			}
	    	return super.rtnDefault(request, response, session, rhs);
		}
		
	    /**
	     * 删除子表记录
	     * @param request
	     * @param response
	     * @param session
	     * @param rhs
	     * @param ep
	     * @return
	     * @throws Exception
	     */
	 
		@RequestMapping( method = RequestMethod.POST)
		public String deleteSub(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs) throws Exception {
	    	String subUuid=getpara("subUuid");
	    	EventPrize ep=(EventPrize)EaCommonUtils.getObjectByUuid(subUuid, "EventPrize");
 
	    	baseService.delete(ep);

	       	return super.redirectWithNewParam(request, response, "deleteSub", "listSub","&mainUuid="+ep.getEvent().getUuid());
		}
	    
	    /**
	     * 进入奖品配置页面
	     * @param request
	     * @param response
	     * @param session
	     * @param rhs
	     * @param eventUuid
	     * @return
	     */
	    @RequestMapping
	    public String manage(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs,String eventUuid){
 
	    	return super.rtnDefault(request, response, session, rhs);
	    	
	    }
	    
	    /**
	     * 加载子表的列表数据
	     * @param request
	     * @param response
	     * @param session
	     * @param rhs
	     * @param eventUuid
	     * @return
	     */
	    @RequestMapping
	    public String listSub(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs,String eventUuid){
			String mainUuid=getpara("mainUuid");
			Event event=(Event)EaCommonUtils.getObjectByUuid(mainUuid, "Event");
			
			List<EventPrize> prizeList=eventPrizeService.getPrizes(event.getId());
 
			rhs.addAttribute("subList", prizeList);
			rhs.addAttribute("event", event);
	    	return super.rtnDefault(request, response, session, rhs);
	    	
	    }
		

		 
}
